import cv2 as cv
import numpy as np

# -----------------------1.仿射变换------------------------------------
img = cv.imread('scenery.png')

rows, cols = img.shape[:2]

# 创建变换矩阵
pts1 = np.float32([[50,50],[200,50],[50,200]])
pts2 = np.float32([[100,100],[200,50],[100,250]])
M = cv.getAffineTransform(pts1, pts2)  # 求得仿射变换矩阵

dst = cv.warpAffine(img, M, (cols, rows))  # 图像，变换矩阵，输出的图像大小。返回变换后的图像

cv.imshow('Original', img)
cv.imshow('Affine', dst)
cv.waitKey(0)
cv.destroyAllWindows()

# ------------------------2.透视变换------------------------------------
img = cv.imread('scenery.png')

rows, cols = img.shape[:2]

# 创建变换矩阵
pts1 = np.float32([[56,65],[368,52],[28,387],[389,390]])
pts2 = np.float32([[0,0],[300,0],[0,300],[300,300]])
M = cv.getPerspectiveTransform(pts1, pts2)  # 求得透视变换矩阵，这个函数支持4点透视变换

dst = cv.warpPerspective(img, M, (cols, rows))  # 图像，变换矩阵，输出图像大小。返回变换后的图像

cv.imshow('Original', img)
cv.imshow('Perspective', dst)
cv.waitKey(0)
cv.destroyAllWindows()

# 下面这串代码是求得实际坐标和像素坐标的变换矩阵M后，用最后的函数实现输入像素坐标，求得未知点的实际坐标
# pixel_points = None
# real_points = None
# M = cv.findHomography(pixel_points,real_points)  # 这个函数支持N个点的透视变换
# image_point = [6.3, 5.9]
# real_point = cv.perspectiveTransform(np.array([image_point]), M)[0]


# ------------------------3.变图像金字塔---------------------
img = cv.imread('rain.jpeg')

up_img = cv.pyrUp(img)  # 上采样,类似于放大
down_img = cv.pyrDown(img)  # 下采样，类似于缩小

cv.imshow('Original', img)
cv.imshow('Up', up_img)
cv.imshow('Down', down_img)
cv.waitKey(0)
cv.destroyAllWindows()  

